""" whichpy - a simple implementation of 'which'

Traverses PATH and prints all files (not just executables!) that match the pattern 
on command line. Probably more useful in finding stuff interactively than 'which',
which only prints the first matching item.

Example:

[lscripts]|91> !whichpy.py *install
D:\WINDOWS\DtcInstall.log
D:\WINDOWS\LUINSTALL.LOG
q:\bin\install.exe
q:\cygwin\bin\install.exe
q:\python25\scripts\easy_install.exe
q:\python25\scripts\pywin32_postinstall.py
q:\python25\scripts\pywin32_postinstall.pyc
q:\python25\scripts\pywin32_postinstall.pyo

"""

import os,sys
from fnmatch import fnmatch
def which(fname):
    fullpath = filter(os.path.isdir,os.environ['PATH'].split(os.pathsep))
    
    if '.' not in fullpath:
        fullpath = ['.'] + fullpath
    fn = fname
    for p in fullpath:
        for f in os.listdir(p):
            head, ext = os.path.splitext(f)
            if f == fn or fnmatch(head, fn):
                yield os.path.join(p,f)
    return
    

def main():    
    for e in which(sys.argv[1]):
        print e

if __name__ == "__main__":
    main()
